**Creates the data for Fig 1(d)

clear all

*global path = "C:/Users/creto/Desktop/chinese_students_2017"



//OPEN LOG FILE
*cap log close
*log using "`logfiles'/prefecture_map_ntrgaps.log", replace


//LOAD MAP DATA
use "map_cd4_xwalk", clear
duplicates report areacode_rev
duplicates drop areacode_rev, force
tempfile map
save "`map'"

/*

//LABELS

use "`mapdata'/CHN_adm_shp/cn2db", clear
/*
sample 10, by(ID_1)
keep id
merge 1:1 id using "`mapdata'/CHN_adm_shp/cn2db"
gen pref_lab = NAME_2 if _merge==3
*/
gen pref_lab = ""
replace pref_lab = NAME_2 if ID_2==8
replace pref_lab = NAME_2 if ID_2==8 //hefei
replace pref_lab = NAME_2 if ID_2==18 //beijing
replace pref_lab = NAME_2 if ID_2==19 //chongqing
replace pref_lab = NAME_2 if ID_2==20 //fuzhou
replace pref_lab = NAME_2 if ID_2==35 //lanzhou
replace pref_lab = NAME_2 if ID_2==46 //guangzhou
replace pref_lab = NAME_2 if ID_2==74 //nanning
replace pref_lab = NAME_2 if ID_2==80 //guiyang
replace pref_lab = NAME_2 if ID_2==87 //haikou
replace pref_lab = NAME_2 if ID_2==97 //shijiazhuang
replace pref_lab = NAME_2 if ID_2==103 //harbin
replace pref_lab = NAME_2 if ID_2==129 //zhengzhou
replace pref_lab = NAME_2 if ID_2==143 //wuhan
replace pref_lab = NAME_2 if ID_2==150 //changsha
replace pref_lab = NAME_2 if ID_2==166 //nanjing
replace pref_lab = NAME_2 if ID_2==181 //nanchang
replace pref_lab = NAME_2 if ID_2==189 //changchun
replace pref_lab = NAME_2 if ID_2==207 //shenyang
replace pref_lab = NAME_2 if ID_2==213 //hohot
replace pref_lab = NAME_2 if ID_2==224 //yinchuan
replace pref_lab = NAME_2 if ID_2==233 //xining
replace pref_lab = NAME_2 if ID_2==240 //xi'an
replace pref_lab = NAME_2 if ID_2==248 //jinan
replace pref_lab = NAME_2 if ID_2==261 //shanghai
replace pref_lab = NAME_2 if ID_2==269 //taiyuan
replace pref_lab = NAME_2 if ID_2==274 //chengdu
replace pref_lab = NAME_2 if ID_2==295 //tianjin
replace pref_lab = NAME_2 if ID_2==298 //urumqi
replace pref_lab = NAME_2 if ID_2==312 //lhasa
replace pref_lab = NAME_2 if ID_2==324 //kunming
replace pref_lab = NAME_2 if ID_2==334 //hangzhou

**manually move some labels apart for visual clarity
replace x_cent = 117 if pref_lab=="Hefei"
replace y_cent = 31.7 if pref_lab=="Hefei"
replace x_cent = 119 if pref_lab=="Nanjing"
replace y_cent = 32.2 if pref_lab=="Nanjing"
replace x_cent = 114.7 if pref_lab=="Shijiazhuang"
replace y_cent = 38.5 if pref_lab=="Shijiazhuang"
replace x_cent = 112.1 if pref_lab=="Taiyuan"
replace y_cent = 37.6 if pref_lab=="Taiyuan"


save "`mapdata'/CHN_adm_shp/prefecturelabels", replace

*/


//LOAD TRADE DATA BY CITY
use "gapsexpweighted97", clear

	describe
	gen cd4 = citycode
	
	tempfile ntrdata
	save "`ntrdata'"
	
use "`map'"	
merge 1:1 cd4 using "`ntrdata'"
	keep if _merge==3
	drop _merge

	merge 1:1 id using "cn2db"
	
	replace ntrgap=0 if ntrgap==.
	
*	format ntrgap_expweighted97  %12.2f
*	replace ntrgap_expweighted97 = 0 if ntrgap_expweighted97==.
*	spmap ntrgap_expweighted97 using "`mapdata'/CHN_adm_shp/cn2coord", id(id)  ///
*			fcolor(Reds2) clmethod(quantile) clnumber(5)  plotregion(color(white)) ///
*			label(data("`mapdata'/CHN_adm_shp/prefecturelabels") xcoord(x_cent) ycoord(y_cent) label(pref_lab) size(*.9 ..) color(blue*.78) pos(12))
*			*polygon(data("`mapdata'/CHN_adm_shp/cn1coord") ocolor(black) osize(medium))
		

		
		
use "gapsexpweighted97", clear

	describe
	gen cd4 = citycode
	
gsort -ntrgap_expweighted97
